package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.EquipmentRepair;
import com.amsp.oa.oademo.mapper.EquipmentRepairMapper;
import com.amsp.oa.oademo.service.EquipmentRepairService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;

@Service
public class EquipmentRepairServiceImpl extends ServiceImpl<EquipmentRepairMapper, EquipmentRepair>
        implements EquipmentRepairService {
    @Override
    public Page<EquipmentRepair> getPage(Integer current, Integer size, String applicationUnit,
            String equipmentNameSpec, String equipmentCode, String applicant,
            LocalDate startDate, LocalDate endDate) {

        LambdaQueryWrapper<EquipmentRepair> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(applicationUnit)) {
            wrapper.like(EquipmentRepair::getApplicationUnit, applicationUnit);
        }

        if (StringUtils.hasText(equipmentNameSpec)) {
            wrapper.like(EquipmentRepair::getEquipmentNameSpec, equipmentNameSpec);
        }

        if (StringUtils.hasText(equipmentCode)) {
            wrapper.like(EquipmentRepair::getEquipmentCode, equipmentCode);
        }

        if (StringUtils.hasText(applicant)) {
            wrapper.like(EquipmentRepair::getApplicant, applicant);
        }

        // 日期范围查询
        if (startDate != null) {
            wrapper.ge(EquipmentRepair::getAcceptanceTime, startDate);
        }

        if (endDate != null) {
            wrapper.le(EquipmentRepair::getAcceptanceTime, endDate);
        }

        // 按创建时间降序排序
        wrapper.orderByDesc(EquipmentRepair::getCreateTime);

        return page(new Page<>(current, size), wrapper);
    }
}